// pages/study/study.js
var languageUtil = require('../../utils/languageUtils')
const db = wx.cloud.database();
var app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    chooseImages: '/images/user.png',
    nickName: '',
    phone: '',
    password: '',
    confirmPassword: '',
    university: '',
    department: '',
    branch: '',
  },
  switchLanguage() {
    //切换当前版本，即修改公共变量中的version
    languageUtil.changLanguage()
   this.initLanguage()
},
//初始化语言
initLanguage() {
  //获取当前小程序语言版本所对应的字典变量
  var lang = languageUtil.languageVersion()
  this.setData({
    content: lang
  })
},
  //实现注册
  addData(){
    //判断用户输入信息有效性；昵称，手机号，密码为必填
    if((this.data.nickName).length == 0){
      wx.showModal({
        title:'错误信息',
        content:'昵称不能为空',
        showCancel: false
      })
      return false
    }

    if((this.data.phone).length == 0){
      wx.showModal({
        title:'错误信息',
        content:'手机号不能为空',
        showCancel: false
      })
      return false
    }

    if((this.data.password).length == 0){
      wx.showModal({
        title:'错误信息',
        content:'密码不能为空',
        showCancel: false
      })
      return false
    }

    if(this.data.password != this.data.confirmPassword){
      wx.showModal({
        title:'错误信息',
        content:'确认密码不一致',
        showCancel: false
      })
      return false
    }

    db.collection('userList').where({
      phone:this.data.phone
    }).get({
      success:function(res){
        if(res.data[0]){
          wx.showModal({
            title:'错误信息',
            content:'该手机号已被注册',
            showCancel: false
          })
          app.globalData.accountUnique = false;
        }else{
          app.globalData.accountUnique = true;
        }
      }
    })
    setTimeout(() => {
      if(app.globalData.accountUnique){
        db.collection('userList').add({
          // data 字段表示需新增的 JSON 数据
          data: {
            // _id: 'todo-identifiant-aleatoire', // 可选自定义 _id，在此处场景下用数据库自动分配的就可以了
            avatarUrl:this.data.chooseImages,
            nickName:this.data.nickName,
            phone:this.data.phone,
            password:this.data.password,
            university:this.data.university,
            department:this.data.department,
            branch:this.data.branch
          },
          success: function(res) {
            // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
            wx.navigateBack({
              delta:1
            })
          }
        })
      }
    }, 1000);
    // var accountUnique =  wx.getStorageSync('flag')
   
  },
  //更换头像
  changePic(){
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success:(res)=> {
        // tempFilePath可以作为img标签的src属性显示图片
        var tempPath = res.tempFilePaths[0]
        this.cloudFile(tempPath)
      }

    })
  },
  // 上传头像
  cloudFile(path){
    wx.cloud.uploadFile({
      cloudPath:Date.now()+".png",
      filePath:path,
      success:res=>{
        this.setData({
          chooseImages:res.fileID
        })
      }
      })
  },
  //获取input
  setNickname:function(e){
    this.setData({
      nickName:e.detail.value
    })
  },
  setPhone:function(e){
    this.setData({
      phone:e.detail.value
    })
  },
  setPassword:function(e){
    this.setData({
      password:e.detail.value
    })
  },
  setconfirmPassword:function(e){
    this.setData({
      confirmPassword:e.detail.value
    })
  },
  setUniversity:function(e){
    this.setData({
      university:e.detail.value
    })
  },  
  setDepartment:function(e){
    this.setData({
      department:e.detail.value
    })
  },
  setBranch:function(e){
    this.setData({
      branch:e.detail.value
    })
  },
  //清除input
  clearInput: function(e) {
    switch (e.currentTarget.id) {
      case 'clear-nickName':
        this.setData({
          nickName: ''
        });
        break;
      case 'clear-phone':
        this.setData({
          phone: ''
        }) ;
        break;
      case 'clear-password':
        this.setData({
          password: ''
        });
        break;
      case 'clear-confirmPassword':
        this.setData({
          confirmPassword: ''
        });
        break;
      case 'clear-university':
        this.setData({
          university: ''
        });
        break;
      case 'clear-department':
        this.setData({
          department: ''
        });
        break;
      case 'clear-branch':
        this.setData({
          branch: ''
        });
        break;  
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initLanguage()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})